package br.ufc.parser;

import java.io.StringReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.parser.CCJSqlParserManager;
import net.sf.jsqlparser.statement.insert.Insert;
import net.sf.jsqlparser.expression.operators.relational.ExpressionList;

public class CustomInsert {

	static final private CCJSqlParserManager parserManager = new CCJSqlParserManager();
	private String sql;
	private List<InsertColumn> columns;
	
	public CustomInsert(String sql){
		this.sql = sql;		
		this.columns = new ArrayList<InsertColumn>();
		this.parse();
	}
	
	private void parse(){		
		try {
			Insert insert = (Insert) parserManager.parse(new StringReader(sql));
			ExpressionList el = (ExpressionList)insert.getItemsList();			
			
//			Iterator<Object> it1 = insert.getColumns().iterator();
			Iterator<String> it1 = Arrays.asList("ycsb_key","field1","field2","field3","field4","field5","field6","field7","field8","field9","field10").iterator();
			Iterator<Object> it2 = el.getExpressions().iterator();
			while(it1.hasNext() && it2.hasNext()) {
				columns.add(new InsertColumn(it1.next().toString(), it2.next().toString()));
			}	
			
		} catch (JSQLParserException e) {
			e.printStackTrace();
		}		
	}
	
	public void print() {
		for(InsertColumn ic : columns){
			System.out.println(ic.toString());
		}
	}

	public List<InsertColumn> getColumns() {
		return columns;
	}	

}
